#include <stdafx.h>
#include <stdio.h>
#include "NFG.h"
#include "Utils.h"

#define MAX_SIZE 256

CNFG::CNFG()
{
}

CNFG::CNFG(char *fileName)
{
	this->LoadFile(fileName);
}

CNFG::~CNFG()
{
	delete []m_aVerticesArray;
	delete []m_aiIndices;
	delete m_aVertices;
}

bool CNFG::LoadFile(char*filename)
{
	FILE * f = fopen(filename, "rt");
	if(!f)
	{
		printf("Error when open the file");
		return false;
	}
	char buffer[MAX_SIZE];
	while(!feof(f))
	{
		ReadLine(f, buffer);

		//Read the number of vertices
		int pos = ScanNumberInt32(buffer, &this->m_iNumOfVertices, 0);
		this->m_aVertices = new VERTEX[this->m_iNumOfVertices + 1];
		this->m_aVerticesArray = new float[this->m_iNumOfVertices*14];

		//Read the info of each vertices
		int index;
		int ID;
		int step = 0;
		for(index = 0; index < this->m_iNumOfVertices; index++)
		{
			//Read next line
			ReadLine(f, buffer);

			//read and skip ID line
			pos = ScanNumberInt32(buffer, &ID, 0);

			//read pos info
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].pos.x, pos);
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].pos.y, pos);
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].pos.z, pos);
			this->m_aVerticesArray[index + step]		= this->m_aVertices[index].pos.x;
			this->m_aVerticesArray[index + step + 1]	= this->m_aVertices[index].pos.y;
			this->m_aVerticesArray[index + step + 2]	= this->m_aVertices[index].pos.z;

			//read normal vector
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].norm.x, pos);
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].norm.y, pos);
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].norm.z, pos);
			this->m_aVerticesArray[index + step + 3]	= this->m_aVertices[index].norm.x;
			this->m_aVerticesArray[index + step + 4]	= this->m_aVertices[index].norm.y;
			this->m_aVerticesArray[index + step + 5]	= this->m_aVertices[index].norm.z;
			
			//read normal vector
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].biNorm.x, pos);
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].biNorm.y, pos);
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].biNorm.z, pos);
			this->m_aVerticesArray[index + step + 6]	= this->m_aVertices[index].biNorm.x;
			this->m_aVerticesArray[index + step + 7]	= this->m_aVertices[index].biNorm.y;
			this->m_aVerticesArray[index + step + 8]	= this->m_aVertices[index].biNorm.z;

			//read tgt info
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].tgt.x, pos);
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].tgt.y, pos);
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].tgt.z, pos);
			this->m_aVerticesArray[index + step + 9]	= this->m_aVertices[index].tgt.x;
			this->m_aVerticesArray[index + step + 10]	= this->m_aVertices[index].tgt.y;
			this->m_aVerticesArray[index + step + 11]	= this->m_aVertices[index].tgt.z;

			//read uv info
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].uv.x, pos);
			pos = ScanNumberFloat32(buffer, &this->m_aVertices[index].uv.y, pos);
			this->m_aVerticesArray[index + step + 12]	= this->m_aVertices[index].uv.x;
			this->m_aVerticesArray[index + step + 13]	= this->m_aVertices[index].uv.y;
			
			step += 13;
		}

		//read number of indices
		ReadLine(f, buffer);

		pos = ScanNumberInt32(buffer, &this->m_iNumOfIndices, 0);
		this->m_aiIndices = new unsigned int[this->m_iNumOfIndices];

		int numFaces = this->m_iNumOfIndices / 3; //num faces

		for(index = 0; index < numFaces; index++)
		{
			ReadLine(f, buffer);

			//skip ID of face
			pos = ScanNumberInt32(buffer, &ID, 0);

			//Read the array of indices
			pos = ScanNumberUInt32(buffer, &this->m_aiIndices[index*3], pos);
			pos = ScanNumberUInt32(buffer, &this->m_aiIndices[index*3 + 1], pos);
			pos = ScanNumberUInt32(buffer, &this->m_aiIndices[index*3 + 2], pos);

		}


		//read successfully
		fclose(f);
		return true;
	}
	return true;
}

unsigned int* CNFG::GetIndices()
{
	return this->m_aiIndices;
}

float* CNFG::GetVertiesArray()
{
	return this->m_aVerticesArray;
}

VERTEX* CNFG::GetVerties()
{
	return this->m_aVertices;
}

int CNFG::GetNumOfVertices()
{
	return this->m_iNumOfVertices;
}

int CNFG::GetNumOfIndices()
{
	return this->m_iNumOfIndices;
}